<template>
	<el-container>
		<el-header>
			<div class="left-panel">
			</div>
			<div class="right-panel">
				<div class="right-panel-search">
					<div class="right-panel-search">
						<el-input v-model="search.username" placeholder="用户名" clearable></el-input>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
					</div>
				</div>
			</div>
		</el-header>
		<el-main class="nopadding">
			<scTable ref="table" :apiObj="apiObj" :params="search" row-key="id"  @selection-change="selectionChange"  hidePagination>
				<el-table-column type="selection" width="50"></el-table-column>
				<el-table-column label="#" type="index" width="50"></el-table-column>
				<el-table-column label="token" prop="token" width="300"></el-table-column>
				<el-table-column label="用户名" prop="username" width="150"></el-table-column>
				<el-table-column label="租户名称" prop="tenantTitle" width="150"></el-table-column>
				<el-table-column label="登录时间" prop="loginTime" width="150"></el-table-column>
				<el-table-column label="ip" prop="ip" width="150"></el-table-column>
				<el-table-column label="设备类型" prop="device" width="150"></el-table-column>
				<el-table-column label="操作" fixed="right" align="right" width="250">
					<template #default="scope">
            <el-button-group>
              <el-button type="primary" @click="checkInfo(scope.row)" text size="small" >查看</el-button>
							<el-popconfirm title="确定切换到这个账号的权限吗？" @confirm="changeAccount(scope.row, scope.$index)" >
								<template #reference>
									<el-button type="primary" text size="small" v-auth="'system.saSession.changeSession'">切换账号(beta)</el-button>
								</template>
							</el-popconfirm>
							<el-popconfirm title="确定将这个账号下线吗？" @confirm="kickOut(scope.row, scope.$index)" >
								<template #reference>
									<el-button type="primary" text size="small" v-auth="'system.saSession.kickOut'">下线</el-button>
								</template>
							</el-popconfirm>
						</el-button-group>
					</template>
				</el-table-column>

			</scTable>
		</el-main>
	</el-container>

	<el-drawer v-model="infoDrawer" title="session详情" :size="1000" destroy-on-close>
		<info ref="info"></info>
	</el-drawer>
</template>

<script>
	import info from './info'
	export default {
		name: 'session-manage',
		components: {
			info
		},
		data() {
			return {
				infoDrawer: false,
				selection: [],
				apiObj: this.$API.system.saSession.list,
				search: {
					username:""
				}
			}
		},
		methods: {
			//单个下线
			async kickOut(row){
				var res = await this.$API.system.saSession.kickOut.delete(row.token);
				if(res.success){
					this.$refs.table.refresh()
					this.$message.success("下线成功")
				}
			},
			//表格选择后回调事件
			selectionChange(selection){
				this.selection = selection;
			},
			//搜索
			upsearch(){
				this.$refs.table.upData(this.search)
			},
			checkInfo(row){
				this.infoDrawer = true
				this.$nextTick(() => {
					this.$refs.info.setData(row)
				})
			},
			async changeAccount(row){
				var res = await this.$API.system.saSession.changeSession.post(row.token);
				if(res.success){
					this.$refs.table.refresh()
					this.$message.success("切换成功")
				}
			},
		},
		mounted() {
		}
	}
</script>

<style>
</style>
